<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Controller;
use excel\Excels as excels;

class Excelout extends Base
{
    // 导出列表
    public function listOut()
    {
        $data = input();//token page_type(1订单列表)

        if (empty($data['page_type'])) {
            apiReturn(201,'页面类型为空');
        }
        if ($data['page_type'] != 4) {
            if (empty($data['ids'])) {
                apiReturn(201,'未勾选');
            }
        }
        //订单列表
        if ($data['page_type'] == 1) {
            $where['a.orderid'] = array('in',$data['ids']);
            $list = db('crm_order')->alias('a')->join('tp_crm_manage b','a.manage_id=b.username')->field('a.*,b.name as manage_name')->where($where)->order('a.date DESC')->select();
            $array[0] = array('订单ID','客户ID','客户姓名','客户电话','客户地址','金额','定金','订单类型','审核状态','订单状态','快递方式','快递单号','支付方式','收款账户','支付单号','下单人','业绩工号','部门分组','班主任','退款金额','渠道','备注','下单时间','审核时间','发货审核','签收拒收时间','客户type值','创建时间');
            $order_status=[1=>'等待支付',2=>'已支付',3=>'待发货',4=>'已发货',5=>'交易成功',6=>'部分退',7=>'拒收',8=>'已作废'];
            $check_status=[0=>'未审核',1=>'经理审核',2=>'质检审核',3=>'财务审核',4=>'确认发货',5=>'确认到审单'];
            $order_type=[0=>'',1=>'正常单',2=>'换货单',3=>'重发单',4=>'异常单',5=>'急发单',6=>'其他单',7=>'无效单'];
            foreach ($list as $key => $value) {
                $customerInfo = db('crm_customer')->field('source,source_type,date')->where('client_id',$value['customer_id'])->find();
                $source = $customerInfo['source'];
                $sourceType = $customerInfo['source_type'];
                $shenhe_time = empty($value['shenhe_time']) ? '' : date("Y-m-d H:i:s",$value['shenhe_time']);
                $fahuo_time = empty($value['fahuo_time']) ? '' : date("Y-m-d H:i:s",$value['shenhe_time']);
                $qianshou_time = empty($value['qianshou_time']) ? '' : date("Y-m-d H:i:s",$value['shenhe_time']);
                $create_time = empty($customerInfo['date']) ? '' : date("Y-m-d H:i:s",$customerInfo['date']);
                $array[] = array($value['orderid'],$value['customer_id'],$value['name'],$value['phone'],$value['address'],$value['total'],$value['ding_price'],$order_type[$value['order_type']],$check_status[$value['check_status']],$order_status[$value['order_status']],$value['express_way'],$value['express_number'],$value['pay_way'],$value['pay_account'],','.$value['pay_sn'],$value['manage_name'],$value['manage_id'],$value['zu_name'],$value['banzhuren_manage_id'],$value['drawback_price'],$source,$value['remark'],date("Y-m-d H:i:s",$value['date']),$shenhe_time,$fahuo_time,$qianshou_time,$sourceType,$create_time);
            }
            //操作记录
            $manage_author = $this->getManageAuthor();
            $this->operationAdd($manage_author['username'], '导出数据', 0, '导出'.count($data['ids']).'个订单');

            excels::index($array,'订单列表 '.date("Y-m-d h.i.s",time()),'','D,E,U,V,W,X,Y',0,12,30,25,30);
        //体验课转支付
        }elseif($data['page_type'] == 2) {
            $where['a.id'] = array('in',$data['ids']);
            $list = db('crm_customer_zhifu')->alias('a')->join('tp_crm_customer b','a.client_id=b.client_id')->field('a.status,a.date,b.client_id,b.name,b.phone,b.grade,b.manage_id,b.source')->where($where)->select();
            $array[0] = array('客户ID','客户姓名','客户电话','年级','来源','归属工号','状态','提交时间');
            $status=[0=>'',1=>'未完成',2=>'已完成'];
            foreach ($list as $key => $value) {
                $array[] = array($value['client_id'],$value['name'],$value['phone'],$value['grade'],$value['source'],$value['manage_id'],$status[$value['status']],date("Y-m-d H:i:s",$value['date']));
            }
            //操作记录
            $manage_author = $this->getManageAuthor();
            $this->operationAdd($manage_author['username'], '导出数据', 0, '导出'.count($data['ids']).'个体验课转支付数据');
            excels::index($array,'体验课转支付列表 '.date("Y-m-d h.i.s",time()),'','C,G',0,12,30,25,30);
        //体验课退款
        }elseif($data['page_type'] == 3) {
            $where['a.id'] = array('in',$data['ids']);
            $list = db('crm_refund')->alias('a')->join('tp_crm_customer b','a.client_id=b.client_id')->field('a.client_id,a.name,a.phone,a.manage_id,a.order_sn,a.status,a.date,b.grade,b.source')->where($where)->select();
            $array[0] = array('客户ID','客户姓名','客户电话','年级','来源','归属工号','支付单号','状态','提交时间');
            $status=[0=>'',1=>'未完成',2=>'已完成'];
            foreach ($list as $key => $value) {
                $array[] = array($value['client_id'],$value['name'],$value['phone'],$value['grade'],$value['source'],$value['manage_id'],$value['order_sn'],$status[$value['status']],date("Y-m-d H:i:s",$value['date']));
            }
            //操作记录
            $manage_author = $this->getManageAuthor();
            $this->operationAdd($manage_author['username'], '导出数据', 0, '导出'.count($data['ids']).'个体验课退款数据');
            excels::index($array,'体验课退款 '.date("Y-m-d h.i.s",time()),'','C,H',0,12,30,25,30);
        //客户列表
        }elseif($data['page_type'] == 4) {
            if (isset($data['zu_id'])) {
                if (!empty($data['zu_id'])) {
                    $wherezu_id = $this->getFenzuList($data['zu_id']);
                    $where['manage_id'] = $this->getManageList($wherezu_id);
                }
            }
            if (isset($data['manage_id'])) {
                if (!empty($data['manage_id'])) {
                    $where['manage_id'] = array('in', $data['manage_id']);
                }
            }
            if (isset($data['name'])) {
                if (!empty($data['name'])) {
                    $where['name'] = array('like','%'.$data['name'].'%');
                }
            }
            if (isset($data['phone'])) {
                if (!empty($data['phone'])) {
                    $where['phone'] = array('like','%'.$data['phone'].'%');
                }
            }
            if (isset($data['grade'])) {
                if (!empty($data['grade'])) {
                    $grade = explode(',', $data['grade']);
                    if (count($grade) == 1) {
                        $where['grade'] = $data['grade'];
                    }else{
                        $where['grade'] = array('in',$data['grade']);
                    }
                }
            }
            if (isset($data['starttime']) && isset($data['endtime'])) {
                if (!empty($data['starttime']) && !empty($data['endtime'])) {
                    $where['date'] = array(array('egt',strtotime($data['starttime'])),array('elt',strtotime($data['endtime'])));
                }
            }
            if (isset($data['zhuanyi_starttime']) && isset($data['zhuanyi_endtime'])) {
                if (!empty($data['zhuanyi_starttime']) && !empty($data['zhuanyi_endtime'])) {
                    $where['zhuanyi_date'] = array(array('egt',strtotime($data['zhuanyi_starttime'])),array('elt',strtotime($data['zhuanyi_endtime'])));
                }
            }
            if (isset($data['paytype_starttime']) && isset($data['paytype_endtime'])) {
                if (!empty($data['paytype_starttime']) && !empty($data['paytype_endtime'])) {
                    $where['paytype_time'] = array(array('egt',strtotime($data['paytype_starttime'])),array('elt',strtotime($data['paytype_endtime'])));
                }
            }
            if (isset($data['fenpei_starttime']) && isset($data['fenpei_endtime'])) {
                if (!empty($data['fenpei_starttime']) && !empty($data['fenpei_endtime'])) {
                    $where['fenpei_date'] = array(array('egt',strtotime($data['fenpei_starttime'])),array('elt',strtotime($data['fenpei_endtime'])));
                }
            }
            if (isset($data['source'])) {
                if (!empty($data['source'])) {
                    $source = explode(',', $data['source']);
                    if (count($source) == 1) {
                        $where['source'] = $data['source'];
                    }else{
                        $where['source'] = array('in',$data['source']);
                    }
                }
            }
            if (isset($data['yaoyue'])) {
                if ($data['yaoyue'] === '') {
                }else{
                    $yaoyue = explode(',', $data['yaoyue']);
                    if (count($yaoyue) == 1) {
                        $where['yaoyue_status'] = $data['yaoyue'];
                    }else{
                        $where['yaoyue_status'] = array('in',$data['yaoyue']);
                    }
                }
            }
            if (isset($data['label'])) {
                if (!empty($data['label'])) {
                    $label = explode(',', $data['label']);
                    if (count($label) == 1) {
                        $where['label'] = $data['label'];
                    }else{
                        $where['label'] = array('in',$data['label']);
                    }
                }
            }
            if (isset($data['xuke_label'])) {
                if (!empty($data['xuke_label'])) {
                    $label = explode(',', $data['xuke_label']);
                    if (count($label) == 1) {
                        $where['xuke_label'] = $data['xuke_label'];
                    }else{
                        $where['xuke_label'] = array('in',$data['xuke_label']);
                    }
                }
            }
            if (isset($data['is_jieye'])) {
                if ($data['is_jieye'] != '') {
                    $where['is_jieye'] = $data['is_jieye'];
                }
            }
            if (isset($data['ticket'])) {
                if ($data['ticket'] !== '') {
                    $where['ticket'] = $data['ticket'];
                }
            }
            if (isset($data['public_paytype'])) {
                if ($data['public_paytype'] !== '') {
                    $where['public_paytype'] = array('in', $data['public_paytype']);
                }
            }
            $customerList = db('crm_customer')->field('client_id,name,phone,grade,source,date,yaoyue_status,source_type,remark')->where($where)->select();

            $yaoyue_status=[0=>'',1=>'已加微信',2=>'已接听未加微',3=>'未接听',4=>'退费',5=>'空号或非本人'];
            $array[0] = array('客户ID','客户姓名','客户电话','年级','来源','创建时间','邀约情况','来源type值','备注');
            foreach ($customerList as $key => $value) {
                $array[] = array($value['client_id'],$value['name'],$value['phone'],$value['grade'],$value['source'],date("Y-m-d H:i:s",$value['date']),$yaoyue_status[$value['yaoyue_status']],$value['source_type'],$value['remark']);
            }
            excels::index($array,'客户列表 '.date("Y-m-d h.i.s",time()),'','C,F',0,12,30,25,30);
        }elseif($data['page_type'] == 5) {
            $where['a.client_id'] = array('in',$data['ids']);
            $where['b.total'] = ['between',[1980,2980]];
            $where['b.order_status'] = array('notin','1,7,8');
            $list = db('crm_customer')->alias('a')->join('tp_crm_order b','a.client_id=b.customer_id')->field('a.client_id,a.name,a.phone,a.grade,a.manage_id,a.date,a.zhuanyi_date,b.orderid,b.manage_id as order_manage_id,b.date as order_date,b.total')->where($where)->select();
            // var_dump(db('crm_customer')->getLastSql());exit;
            $array[0] = array('归属工号','客户ID','客户姓名','客户电话','年级','分配时间','下单人','下单金额');
            foreach ($list as $key => $value) {
                $manage_name = db('crm_manage')->where('username',$value['manage_id'])->value('name');
                $order_manage_name = db('crm_manage')->where('username',$value['order_manage_id'])->value('name');
                $array[] = array($value['manage_id'].':'.$manage_name,$value['client_id'],$value['name'],$value['phone'],$value['grade'],date("Y-m-d H:i:s",$value['zhuanyi_date']),$value['order_manage_id'].':'.$order_manage_name,$value['total']);
            }
            excels::index($array,'客户列表 '.date("Y-m-d h.i.s",time()),'','A,D,F,G',0,12,30,35,30);
        }else{
            apiReturn(201,'页面类型错误');
        }
    }

}
